#include "audiofile.h"
#include "playlistentry.h"
#include "tagreaderthread.h"

TagReaderThread::TagReaderThread(QObject *parent) :
    QThread(parent)
{
}

bool TagReaderThread::readTags(const QList<AudioFile*> &i_files)
{
    if (isRunning())
        return false;

    m_files = i_files;
    start();
    return true;
}

bool TagReaderThread::refreshEntries(const QList<PlaylistEntry *> &i_entries)
{
    if (isRunning())
        return false;

    m_entries = i_entries;
    start();
    return true;
}

void TagReaderThread::run()
{
    if (m_entries.isEmpty())
    {
        // Tags abrufen
        const int count = m_files.size();

        for (int i = 0; i < count && !isInterruptionRequested(); i++)
        {
            emit progressChanged(i, count);
            m_files.at(i)->readTags(true);
        }
    }
    else
    {
        // Entries aktualisieren
        const int count = m_entries.size();

        for (int i = 0; i < count && !isInterruptionRequested(); i++)
        {
            emit progressChanged(i, count);
            m_entries.at(i)->refreshTags();
        }

        m_entries.clear();
    }
}
